/************************************************************
 * $Id : CmdDtmf.cpp   2011/08/01
 *
 *       swhors@naver.com
 ************************************************************/

#include "config.h"

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
 
using namespace std;

#include "constant.h"
#include "ast_peg.h"
#include "ast_db.h"
#include "CmdBase.h"
#include "CmdDtmf.h"
#include "ast_event.h"

#include "SkyConfUtil.h"

extern int sendCommand( char * aFmt, ...);

extern int gIsConnected2DB;

CCmdDtmf::CCmdDtmf()
{
   init( );
}

CCmdDtmf::~CCmdDtmf()
{
   init( );
}

void CCmdDtmf::release()
{
   super::release();
}

void CCmdDtmf::init()
{
   super::init();
   
   memset( mConferenceName, 0, 32 );
   memset( mUniqueId,       0, 32 );
   memset( mCallerId,       0, 32 );
   memset( mCallerIdName ,  0, 32 );
   memset( mFlags,          0, 32 );
   
   mKey   = 0;
   mCount = 0;
   mMute  = 0;
}

void * CCmdDtmf::parse(char * aCommandStr )
{
   char * sStart       = NULL;
   char * sMsgStartPtr = NULL;
   char   sBuff[32]    = {0,};
   
   sMsgStartPtr = aCommandStr;

   super::parse( sMsgStartPtr );
   
   sMsgStartPtr = aCommandStr;
   
   if( callerid_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset( mCallerId, 0, 32  ); 
      memcpy( mCallerId, sStart + 10, ( sMsgStartPtr - sStart - 12 )  );
   }
   else
   {
      /* do nothing */
   }

   if( calleridname_f::Matches(sMsgStartPtr, sStart) )
   {
      memset( mCallerIdName, 0, 32  ); 
      memcpy( mCallerIdName, sStart + 14, ( sMsgStartPtr - sStart - 16 )  );
   }
   else
   {
      /* do nothing */
   }

   if( key_f::Matches( sMsgStartPtr, sStart ) )
   {
      mKey = 0;
      mKey = *( sStart + 5  );
   }
   else
   {
      /* do nothing */
   }

   if( count_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset( sBuff, 0, 32  ); 
      memcpy( sBuff, sStart + 7, ( sMsgStartPtr- sStart- 9 )  );
      mCount = atoi( ( char* ) &sBuff  );
   }
   else
   {
      /* do nothing */
   }

   if( flags_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset( mFlags, 0, 32  ); 
      memcpy( mFlags, sStart + 7, ( sMsgStartPtr - sStart - 9 )  );
   }
   else
   {
      /* do nothing */
   }

   if( mute_f::Matches( sMsgStartPtr, sStart ) )
   {
      memset( sBuff, 0, 32  ); 
      memcpy( sBuff, sStart + 6, ( sMsgStartPtr - sStart - 8 )  );
      mMute = atoi( ( char* ) &sBuff  );
   }
   else
   {
      /* do nothing */
   }

   return this;
}
CCmdDtmf & CCmdDtmf::operator = ( const CCmdDtmf &aOther )
{
   sprintf( mPrivilege,     "%s", aOther.mPrivilege );
   sprintf( mConferenceName,"%s", aOther.mConferenceName );
   sprintf( mUniqueId,      "%s", aOther.mUniqueId );
   sprintf( mChannel,       "%s", aOther.mChannel );
   sprintf( mCallerId,      "%s", aOther.mCallerId );
   sprintf( mCallerIdName,  "%s", aOther.mCallerIdName );
   sprintf( mFlags,         "%s", aOther.mFlags );
   
   mKey   = aOther.mKey;
   mCount = aOther.mCount;
   mMute  = aOther.mMute;
   
   return *this;
}

int CCmdDtmf::operator<( const CCmdDtmf & aOther )
{
   if( strcmp( mPrivilege, aOther.mPrivilege ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( strcmp( mConferenceName, aOther.mConferenceName ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( strcmp( mUniqueId, aOther.mUniqueId ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( strcmp( mChannel, aOther.mChannel ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( strcmp( mCallerId, aOther.mCallerId ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
    
   if( strcmp( mCallerIdName,aOther.mCallerIdName ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( mKey < aOther.mKey )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( mCount < aOther.mCount )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( strcmp(  mFlags, aOther.mFlags ) < 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   if( mMute < aOther.mMute )
   {
      /* do nothing */
   }
   else
   {
      goto notless;
   }
   
   return 1;

notless:
   
   return 0;
}

int CCmdDtmf::operator>(const CCmdDtmf& aOther)
{
   if( strcmp(  mPrivilege, aOther.mPrivilege ) > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp(  mConferenceName, aOther.mConferenceName ) > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp(  mUniqueId, aOther.mUniqueId ) > 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp( mChannel, aOther.mChannel) > 0)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp( mCallerId, aOther.mCallerId) > 0)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp( mCallerIdName,aOther.mCallerIdName) > 0)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( mKey > aOther.mKey)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( mCount > aOther.mCount)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( strcmp( mFlags, aOther.mFlags) > 0)
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   if( mMute > aOther.mMute )
   {
      /* do nothing */
   }
   else
   {
      goto notmore;
   }
   
   return 1;
   
notmore:
   return 0;
}

int CCmdDtmf::operator == ( const CCmdDtmf& aOther )
{
   if( strcmp( mPrivilege, aOther.mPrivilege ) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   if( strcmp( mChannel, aOther.mChannel ) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( strcmp( mConferenceName, aOther.mConferenceName) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( strcmp( mUniqueId, aOther.mUniqueId) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( strcmp( mCallerId, aOther.mCallerId) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( strcmp( mCallerIdName,aOther.mCallerIdName) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( mKey == aOther.mKey)
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( mCount == aOther.mCount)
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }

   if( strcmp( mFlags, aOther.mFlags) == 0 )
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   if( mMute == aOther.mMute)
   {
      /* do nothing */
   }
   else
   {
      goto notequal;
   }
   
   return 1;
   
notequal:
   
   return 0;
}

int CCmdDtmf::event()
{
   int  sRet      = 0;
   int  sRetCode  = 0;
   char sOutChanName[USERNAME_MAX_LEN];
   char sChannel[64];

   memset( sChannel, 0, sizeof( char ) * 64 );
   
   if( gIsConnected2DB )
   {
      
      memset( sOutChanName, 0, sizeof(char) * USERNAME_MAX_LEN );
      
      GetConfOutChanName( sOutChanName, USERNAME_MAX_LEN );
      
      sRet = online_sql_get_customer( mConferenceName,
                                      sOutChanName,
			                          sChannel,
                                      64 );
      if( sRet > 0 )
      {
         switch(mKey)
         {
         case '#':
            // call transfer 
            break;
         case '*':
            // record
            start_recording( mConferenceName);
            sRetCode = sendCommand( (char *)DTMF_CMD_RECORD, sChannel );
            break;
         case '0':
            // kickout
            sRetCode = sendCommand( (char *)DTMF_CMD_KICKOUT, sChannel );
            break;
         case '5':
            // mute
            sRetCode = sendCommand( (char *)DTMF_CMD_MUTE, sChannel );
            break;
         case '6':
            // un-mute
            sRetCode = sendCommand( (char *)DTMF_CMD_UNMUTE, sChannel );
            break;
         default:
            break;
         }
         goto event_success;
      }
      else
      {
         /* do nothing */
      }
   }
   else
   {
      /* do nothing */
   }
   
   goto event_fail;
   
event_success:
   return sRetCode;
   
event_fail:
   return 0;
}

